package com.tcm.smarthealth.controller;

import com.tcm.smarthealth.common.ApiResponse;
import com.tcm.smarthealth.common.exception.BusinessException;
import com.tcm.smarthealth.dto.dashboard.DashboardOverviewResponse;
import com.tcm.smarthealth.security.AuthContext;
import com.tcm.smarthealth.security.AuthenticatedUser;
import com.tcm.smarthealth.service.DashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Dashboard", description = "首页概览数据接口")
@RestController
@RequestMapping("/api/v1/dashboard")
public class DashboardController {

    private final DashboardService dashboardService;

    public DashboardController(DashboardService dashboardService) {
        this.dashboardService = dashboardService;
    }

    @Operation(summary = "获取首页概览数据")
    @GetMapping("/overview")
    public ApiResponse<DashboardOverviewResponse> overview() {
        AuthenticatedUser currentUser = requireLogin();
        DashboardOverviewResponse overview = dashboardService.fetchOverview(currentUser.getId());
        return ApiResponse.success(overview);
    }

    private AuthenticatedUser requireLogin() {
        AuthenticatedUser user = AuthContext.getCurrentUser();
        if (user == null) {
            throw new BusinessException(HttpStatus.UNAUTHORIZED, "登录已失效，请重新登录");
        }
        return user;
    }
}


